package com.ycsj.goldmedalnewconcept.student.activity;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.OnloadingMoreListener;
import com.ycsj.common.base.SpKeys;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.CustomDialogManager;
import com.ycsj.common.manager.PopUpManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.common.utils.ArithUtil;
import com.ycsj.common.utils.DateUtils;
import com.ycsj.common.utils.SPUtil;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.ResultInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.common.util.StateCodeUtils;
import com.ycsj.goldmedalnewconcept.student.adapter.DjRvAdapter;
import com.ycsj.goldmedalnewconcept.student.adapter.DtRvAdapter;
import com.ycsj.goldmedalnewconcept.student.adapter.HyRvAdapter;
import com.ycsj.goldmedalnewconcept.student.bean.DjInfo;
import com.ycsj.goldmedalnewconcept.student.bean.DtInfo;
import com.ycsj.goldmedalnewconcept.student.bean.HomeInfo;
import com.ycsj.goldmedalnewconcept.student.bean.HyInfo;
import com.ycsj.goldmedalnewconcept.student.bean.UseCardRes;
import com.ycsj.goldmedalnewconcept.student.bean.Water;
import com.ycsj.goldmedalnewconcept.view.WaterView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的家界面
 * Created by Administrator on 2019/2/28 0028.
 */

public class StudentMyHomeActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.ll_top)
    LinearLayout llTop;
    @BindView(R.id.ll_out)
    LinearLayout llOut;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_pb)
    TextView tvPb;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.pb)
    ProgressBar pb;
    @BindView(R.id.tv_levle)
    TextView tvLevle;
    @BindView(R.id.tv_jy)
    TextView tvJy;
    @BindView(R.id.bt_gn)
    Button btGn;
    @BindView(R.id.bt_dj)
    Button btDj;
    @BindView(R.id.bt_hy)
    Button btHy;
    @BindView(R.id.bt_dt)
    Button btDt;
    @BindView(R.id.wv_water)
    WaterView waterView;
    @BindView(R.id.lv_cw)
    ImageView lvCw;
    //内容布局
    @BindView(R.id.rl_content)
    RelativeLayout rlContent;
    //加载框布局
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    //牛奶控件的数据集合
    private List<Water> mWaters;

    //宠物对应的发呆和开心的gif图数据  0狗 1怪物 2老虎 3大象
    private int[] gif_fd = {R.drawable.xg_fd, R.drawable.xgw_fd, R.drawable.xlh_fd, R.drawable.xx_fd};

    private int[] gif_kx = {R.drawable.xg_kx, R.drawable.xgw_kx, R.drawable.xlh_kx, R.drawable.xx_kx};


    //看书的gif动画
    private int[] gif_ks = {R.drawable.xg_ks, R.drawable.xgw_ks, R.drawable.xlh_ks, R.drawable.xx_ks};

    //记录需要展示宠物的索引
    private int index;

    //请求的参数
    private String acc, acc2, role, apptoken;

    //空视图
    private View emptyView;
    //空视图的提示语信息
    private TextView emptySmg;

    //弹框相关的视图
    private RecyclerView rv;
    private RelativeLayout rlPopLoading;


    //动态的适配器对象
    private DjRvAdapter dtRvAdapter;
    //道具的适配器对象
    private DtRvAdapter djRvAdapter;
    private SmartRefreshLayout srf;
    private LinearLayout llPopContent;
    private TextView tvPopName;
    //请求道具数据的翻页
    private int dtPage = 0;

    //动态的数据
    private List<DtInfo.DataBean> dtInfos;

    //道具的数据
    private List<DjInfo.DataBean> djInfos;

    //好友的数据
    private List<HyInfo.DataBean> hyInfos;

    //好友的适配器对象
    private HyRvAdapter hyRvAdapter;

    //Handler对象
    private Handler handler = new Handler();

    //是否是领取牛奶后刷新动画
    private boolean isGetMilk;


    //Gilde的过渡动画
    private DrawableCrossFadeFactory drawableCrossFadeFactory = new DrawableCrossFadeFactory.Builder(300).setCrossFadeEnabled(true).build();


    @Override
    protected int getContentLayout() {
        return R.layout.activity_student_myhome;
    }

    @Override
    protected void setTitle() {
        super.setTitle();
        tvTitle.setText("我的家");
    }


    @Override
    protected void init() {

        initStateBar();

        //初始化请求参数
        initParams();

        //根据性别设置背景
        initBg();

        //请求数据
        http();
    }

    /**
     * 根据性别设置背景
     */
    private void initBg() {

        String sex = SPUtil.getString(this, SpKeys.SEX);

        //背景有st_home_bg 女 和friend_home_bg 男  还有空背景
        if ("1".equals(sex)) {
            llOut.setBackgroundResource(R.drawable.friend_home_bg);
        } else if ("2".equals(sex)) {
            llOut.setBackgroundResource(R.drawable.friend_home_bg);
        } else {
            llOut.setBackgroundResource(R.drawable.friend_home_bg);
        }
    }

    /**
     * 配置请求参数
     */
    private void initParams() {
        acc = AccountUtils.getAccount(this);
        acc2 = AccountUtils.getAccount(this);
        role = AccountUtils.getRole(this);
        apptoken = AccountUtils.getToken(this);
    }

    /**
     * 请求服务器数据
     */
    private void http() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "6");
        map.put("apptoken", apptoken);
        map.put("platform", "2");
        map.put("acc2", acc2);
        map.put("acc", acc);
        map.put("role", role);

//        LogUtils.iTag("test", HttpAddress.HOMEINFO + "?type=6&platform=2&apptoken=" +
//                apptoken + "&acc2=" + acc2 + "&acc=" + acc + "&role=" + role);

        HttpUtils.post(HttpAddress.HOMEINFO, map, this, new HttpCallBack<HomeInfo>() {
            @Override
            public void onSuccess(HomeInfo homeInfo) {
                rlLoading.setVisibility(View.GONE);
                rlContent.setVisibility(View.VISIBLE);
                if ("200".equals(homeInfo.getState())) {
                    //设置页面数据
                    setPageData(homeInfo);
                } else if ("104".equals(homeInfo.getState())) {
                    //没有可采集牛奶的数据
                    setPetInfo(homeInfo);
                } else {
                    StateCodeUtils.showSateCode(homeInfo.getState());
                }
            }

            @Override
            public void onError(String e) {
                rlLoading.setVisibility(View.GONE);
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });
    }

    /**
     * 设置页面数据
     *
     * @param homeInfo
     */
    private void setPageData(HomeInfo homeInfo) {

        //设置宠物信息
        setPetInfo(homeInfo);

        //设置未收取的牛奶数据
        List<HomeInfo.DataBean> data = homeInfo.getData();
        if (data == null || data.size() == 0) {
            //表示没有收取的牛奶
            waterView.setVisibility(View.GONE);
        } else {
            waterView.setVisibility(View.VISIBLE);
            mWaters = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                Water water = new Water();
                water.setId(data.get(i).getId());
                water.setMilkBeginTime(data.get(i).getMilkBeginTimeStr());
                water.setMilkEndTime(data.get(i).getMilkEndTimeStr());
                water.setName(data.get(i).getMilkName());
                water.setNumber(Integer.parseInt(data.get(i).getMilkMass()));
                mWaters.add(water);
            }
            waterView.setWaters(mWaters);
            waterView.setOnWaterClickListener(new WaterView.onWaterClickListener() {
                @Override
                public void onWaterClick(Water water, View view) {

                    //请求服务器领取牛奶
                    httpGetMilkAciton(water, view);
                }
            });

        }
    }

    /**
     * 设置宠物的相信信息
     *
     * @param homeInfo
     */
    private void setPetInfo(final HomeInfo homeInfo) {
        //根据宠物的id设置宠物
        final String petId = homeInfo.getPET_ID();
        index = Integer.parseInt(petId);

        //判断是否需要展示看书的GIF
        if (!isGetMilk) {
            //初次进入时候加载动画
            if ("1".equals(homeInfo.getHasGif())) {
                //设置展示道具Gif的ImageView的大小和定时取消
                sysCwState(petId, homeInfo.getPetGifEndTime());
            } else {
                //没有道具,需要加载默认的Gif图片

                setLvCwParams(UiUtils.dp2px(220), UiUtils.dp2px(220));
                Glide.with(this).load(gif_fd[index]).transition(DrawableTransitionOptions.with(drawableCrossFadeFactory)).into(lvCw);
            }

        } else {
            //领取牛奶后加载动画
            handler.removeCallbacksAndMessages(null);

            //开心的动画
            Glide.with(this).load(gif_kx[index]).transition(DrawableTransitionOptions.with(drawableCrossFadeFactory)).into(lvCw);

            //5秒后执行
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //5秒后还原宠物动画
                    if ("1".equals(homeInfo.getHasGif())) {
                        //设置展示道具Gif的ImageView的大小和定时取消
                        sysCwState(petId, homeInfo.getPetGifEndTime());
                    } else {
                        //没有道具,需要加载默认的Gif图片
                        setLvCwParams(UiUtils.dp2px(220), UiUtils.dp2px(220));
                        Glide.with(StudentMyHomeActivity.this).load(gif_fd[index])
                                .transition(DrawableTransitionOptions.with(drawableCrossFadeFactory)).into(lvCw);
                    }
                }
            }, 3000);
        }


        //设置宠物名称,等级,进度以及牛奶数量
        String nickName = homeInfo.getNICK_NAME();
        tvName.setText(nickName);

        //获取宠物的等级
        String petLevel = homeInfo.getLEVEL();
        tvLevle.setText("Lv." + petLevel);

        //获取当前的牛奶
        String currMilk = homeInfo.getMILK();

        //获取升下一级需要的牛奶
        String nextMilk = homeInfo.getNEXT_MILK();
        tvPb.setText(currMilk + "/" + nextMilk);

        //设置进度
        pb.setProgress(ArithUtil.bs(Integer.parseInt(currMilk), Integer.parseInt(nextMilk)));

        //获取总的牛奶数
        String milk = homeInfo.getMILK();
        tvJy.setText(milk + "g");
    }


    /**
     * 设置展示宠物的ImageView的布局参数
     */
    private void setLvCwParams(int width, int height) {
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, height);
        lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        lp.bottomMargin = UiUtils.dp2px(26);
        lvCw.setLayoutParams(lp);
    }

    /**
     * 领取自己的牛奶
     *
     * @param water
     */
    private void httpGetMilkAciton(Water water, final View view) {
        CustomDialogManager.show(this, "领取中...");
        Map<String, String> map = new HashMap<>();
        map.put("type", "2");
        map.put("acc", acc);
        map.put("role", role);
        map.put("apptoken", apptoken);
        map.put("platform", "2");
        map.put("id", water.getId());
        HttpUtils.post(HttpAddress.GETMILK, map, this, new HttpCallBack<ResultInfo>() {
            @Override
            public void onSuccess(ResultInfo resultInfo) {
                CustomDialogManager.dissmiss();
                if ("200".equals(resultInfo.getState())) {
//                    ToastUtils.showShort("领取成功");
                    //领取成功后执行消失的动画
                    waterView.handViewClick(view);

                    //改变领取牛奶的标识
                    isGetMilk = true;

                    //领取牛奶成功后执行开心的gif动画,5秒后还原为本来应该执行的动画
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //成功后刷新数据
                            http();
                        }
                    }, 800);

                } else {
                    StateCodeUtils.showSateCode(resultInfo.getState());
                }
            }

            @Override
            public void onError(String e) {
                CustomDialogManager.dissmiss();
                ToastUtils.showShort(BaseApplication.ERROR + ",领取失败");
            }
        });


    }


    /**
     * 页面控件点击事件
     */
    @OnClick({R.id.iv_back, R.id.bt_gn, R.id.bt_dj, R.id.bt_hy, R.id.bt_dt})
    public void onViewClicked(View view) {
        if (!isDoubleClick(view)) {

            switch (view.getId()) {
                case R.id.iv_back:
                    //返回
                    finish();
                    break;
                case R.id.bt_gn:
                    //攻略
                    UiUtils.startActivity(this, PetHelperActivity.class);
                    break;
                case R.id.bt_dj:
                    //道具
                    showDjPop();
                    break;
                case R.id.bt_hy:
                    //好友
                    showHyPop();
                    break;
                case R.id.bt_dt:
                    //动态
                    showDtPop();
                    break;
            }
        }
    }


    /**
     * 动态的弹框
     */
    private void showDtPop() {
        //请求服务器的道具数据
        PopUpManager.showPop(this, R.layout.pop_gy, 0.3f, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {

                //内容布局
                llPopContent = view.findViewById(R.id.ll_content);

                //标题
                tvPopName = view.findViewById(R.id.tv_name);
                tvPopName.setText("动态");

                //下拉刷新
                srf = view.findViewById(R.id.srf);

                SrfManager.setSmartByPullListener(srf, new SrfManager.OnPullRefreshListener() {
                    @Override
                    public void onRefresh(RefreshLayout refreshlayout) {
                        //下拉刷新
                        HttpDt(BqaManager.PULLREFRESH);
                    }
                });
                //关闭按钮
                ImageView lvClose = view.findViewById(R.id.lv_close);

                lvClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                    }
                });

                //列表控件
                rv = view.findViewById(R.id.rv);

                //加载框布局
                rlPopLoading = view.findViewById(R.id.rl_loading);

                //加载的空视图
                emptyView = View.inflate(StudentMyHomeActivity.this, R.layout.base_rv_common_empty_view_white_bg, null);
                emptySmg = emptyView.findViewById(R.id.tv_content);
                dtInfos = new ArrayList<>();
                dtRvAdapter = new DjRvAdapter(R.layout.item_rv_dj, dtInfos);


                //请求数据
                HttpDt(BqaManager.NORML);
            }
        });
    }

    /**
     * 请求动态的数据
     *
     * @param method
     */
    private void HttpDt(final int method) {
        if (method == BqaManager.NORML || method == BqaManager.PULLREFRESH) {
            dtPage = 0;
        } else {
            dtPage++;
        }

        Map<String, String> map = new HashMap<>();
        map.put("type", "7");
        map.put("acc", acc);
        map.put("role", role);
        map.put("apptoken", apptoken);
        map.put("platform", "2");
        map.put("page", dtPage + "");
        map.put("size", 5 + "");
        map.put("friend", acc);

        HttpUtils.post(HttpAddress.DTDATA, map, this, new HttpCallBack<DtInfo>() {
            @Override
            public void onSuccess(DtInfo info) {
                rlPopLoading.setVisibility(View.GONE);
                llPopContent.setVisibility(View.VISIBLE);
                if ("200".equals(info.getState())) {
                    List<DtInfo.DataBean> newDatas = info.getData();
                    if (method == BqaManager.NORML) {
                        if (newDatas != null && newDatas.size() != 0) {
                            dtInfos.addAll(newDatas);
                        }
                        BqaManager.setRv(emptyView, StudentMyHomeActivity.this, dtRvAdapter, rv, new OnloadingMoreListener() {
                            @Override
                            public void onLoadMore() {
                                HttpDt(BqaManager.LOADINDGMORE);
                            }
                        });
                    } else {
                        BqaManager.updateRvBySmf(method, srf, rv, dtInfos, newDatas, dtRvAdapter);
                    }
                } else if ("104".equals(info.getState())) {
                    //没查询到数据
                    if (method == BqaManager.LOADINDGMORE) {
                        //结束加载更多
                        dtRvAdapter.loadMoreEnd();
                    } else if (method == BqaManager.NORML) {
                        emptySmg.setText("暂无动态");
                        BqaManager.setRv(emptyView, StudentMyHomeActivity.this, dtRvAdapter, rv, new OnloadingMoreListener() {
                            @Override
                            public void onLoadMore() {
                                HttpDt(BqaManager.LOADINDGMORE);
                            }
                        });
                    } else if (method == BqaManager.PULLREFRESH) {
                        if (srf != null) {
                            srf.finishRefresh();
                        }
                        dtInfos.clear();
                        dtRvAdapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtils.showShort(BaseApplication.STATEERROE + info.getState());
                }
            }

            @Override
            public void onError(String e) {
                rlPopLoading.setVisibility(View.GONE);
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });

    }

    /**
     * 请求道具的数据
     */
    private void httpDj() {

        Map<String, String> map = new HashMap<>();
        map.put("type", "12");
        map.put("acc", acc);
        map.put("role", role);
        map.put("apptoken", apptoken);
        map.put("platform", "2");

        HttpUtils.post(HttpAddress.DJDATA, map, this, new HttpCallBack<DjInfo>() {
            @Override
            public void onSuccess(DjInfo info) {

                rlPopLoading.setVisibility(View.GONE);
                llPopContent.setVisibility(View.VISIBLE);

                if ("200".equals(info.getState())) {
                    List<DjInfo.DataBean> beans = info.getData();
                    if (beans != null) {
                        djInfos = new ArrayList<>();
                        djInfos.addAll(beans);
                        djRvAdapter = new DtRvAdapter(StudentMyHomeActivity.this, R.layout.item_rv_dt, djInfos);
                        emptySmg = emptyView.findViewById(R.id.tv_content);
                        LinearLayoutManager manager = new LinearLayoutManager(StudentMyHomeActivity.this, LinearLayoutManager.HORIZONTAL, false);
                        emptySmg.setText("暂无道具");
                        BqaManager.setRv(emptyView, manager, StudentMyHomeActivity.this, djRvAdapter, rv);

                        djRvAdapter.setOnClickUseDjListener(new DtRvAdapter.OnClickUseDjListener() {
                            @Override
                            public void onUseDj(DjInfo.DataBean item) {
                                //使用道具
                                if (Integer.parseInt(item.getAmount()) > 0) {

                                    //先关闭第一个弹框
                                    PopUpManager.dismiss();

                                    //表示学习卡还有使用机会
                                    ShowUseCarePop(item);

                                } else {
                                    ToastUtils.showShort("暂无可以使用的学习卡");
                                }
                            }
                        });
                    } else {
                        LogUtils.iTag("test", Constants.EMPTY);
                    }

                } else {
                    LogUtils.iTag("test", Constants.ERRORSTATE + info.getState());
                    StateCodeUtils.showSateCode(info.getState());
                }

            }

            @Override
            public void onError(String e) {
                rlPopLoading.setVisibility(View.GONE);
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });

    }

    /**
     * 弹出使用道具的弹框
     *
     * @param item
     */
    private void ShowUseCarePop(final DjInfo.DataBean item) {
        PopUpManager.showPop(this, R.layout.pop_use_card, 0.3f, this, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                //提示信息
                TextView tvContent = view.findViewById(R.id.tv_content);
                tvContent.setText(item.getPurpose());

                //取消
                TextView tvCancel = view.findViewById(R.id.tv_cancel);

                //使用
                TextView tvUse = view.findViewById(R.id.tv_use);
                tvUse.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        //请求使用道具的接口
                        PopUpManager.dismiss();

                        CustomDialogManager.show(StudentMyHomeActivity.this, "使用中...");

                        httpUseCard(item);
                    }
                });


                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                    }
                });
            }
        });
    }

    /**
     * 使用道具
     */
    private void httpUseCard(DjInfo.DataBean item) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "13");
        map.put("acc", acc);
        map.put("role", role);
        map.put("apptoken", apptoken);
        map.put("platform", "2");
        map.put("id", item.getId() + "");
        map.put("status", "0");
        map.put("amount", (Integer.parseInt(item.getAmount()) - 1) + "");

        HttpUtils.post(HttpAddress.USECARD, map, this, new HttpCallBack<UseCardRes>() {
            @Override
            public void onSuccess(UseCardRes resultInfo) {
                CustomDialogManager.dissmiss();
                if ("200".equals(resultInfo.getState())) {
                    //使用成功
                    PopUpManager.dismiss();
                    ToastUtils.showShort("使用道具成功");

                    //同时切换宠物的图片相对应的宠物GIF图片
                    if (null != resultInfo.getDate()) {
                        Log.i("test", "学习的petID=" + resultInfo.getDate().petGifType);
                        sysCwState(index + "", resultInfo.getDate().getPetGifEndTime());

                    }

                } else {
                    StateCodeUtils.showSateCode(resultInfo.getState());
                }
            }

            @Override
            public void onError(String e) {
                //使用失败
                CustomDialogManager.dissmiss();
                ToastUtils.showShort(BaseApplication.ERROR + ",使用道具失败");
            }
        });
    }


    /**
     * 使用道具后同步展示宠物Gif动图
     */
    private void sysCwState(final String petId, String petGifEndTime) {
        handler.removeCallbacksAndMessages(null);


        //Gif图需要显示的图片比较大
        setLvCwParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        //表示有道具,需要显示看书的Gif图
        Glide.with(this).load(gif_ks[index]).transition(DrawableTransitionOptions.with(drawableCrossFadeFactory)).into(lvCw);

        //获取Gif图结束时间与当前系统时间差
        long time = DateUtils.getTime(petGifEndTime, "yyyy-MM-dd HH:mm:ss") - System.currentTimeMillis();

        //当道具的Gif到时间后不在显示道具的Gif图,而是显示默认的Gif图
        //先移除handler的任务,在添加任务
        handler.removeCallbacksAndMessages(null);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Glide.with(StudentMyHomeActivity.this).load(gif_fd[index]).transition(DrawableTransitionOptions.with(drawableCrossFadeFactory)).into(lvCw);
            }
        }, time);
    }

    /**
     * 好友的弹框
     */
    private void showHyPop() {

        //请求服务器的道具数据
        PopUpManager.showPop(this, R.layout.pop_gy, 0.3f, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                //关闭按钮
                ImageView lvClose = view.findViewById(R.id.lv_close);
                lvClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                    }
                });

                //内容布局
                llPopContent = view.findViewById(R.id.ll_content);

                //标题
                tvPopName = view.findViewById(R.id.tv_name);
                tvPopName.setText("好友");

                //下拉刷新
                srf = view.findViewById(R.id.srf);
                SrfManager.setSmartByPullListener(srf, null);

                //列表控件
                rv = view.findViewById(R.id.rv);

                //加载框布局
                rlPopLoading = view.findViewById(R.id.rl_loading);

                //加载的空视图
                emptyView = View.inflate(StudentMyHomeActivity.this, R.layout.base_rv_common_empty_view_white_bg, null);
                emptySmg = emptyView.findViewById(R.id.tv_content);

                //请求好友的数据
                httpHyData();
            }
        });
    }

    /**
     * 请求好友的数据
     */
    private void httpHyData() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "11");
        map.put("acc", acc);
        map.put("role", role);
        map.put("apptoken", apptoken);
        map.put("platform", "2");
//        LogUtils.iTag("test", HttpAddress.FRIENDDATA + "?type=11&platform=2&acc=" + acc + "&role="
//                + role + "&apptoken=" + apptoken);
        HttpUtils.post(HttpAddress.FRIENDDATA, map, this, new HttpCallBack<HyInfo>() {
            @Override
            public void onSuccess(HyInfo hyInfo) {
                rlPopLoading.setVisibility(View.GONE);
                llPopContent.setVisibility(View.VISIBLE);
                if ("200".equals(hyInfo.getState())) {
                    List<HyInfo.DataBean> dataBeans = hyInfo.getData();
                    if (null == dataBeans) {
                        LogUtils.iTag("test", Constants.EMPTY);
                    } else {
                        hyInfos = dataBeans;
                        emptySmg.setText("暂无好友");
                        hyRvAdapter = new HyRvAdapter(StudentMyHomeActivity.this, R.layout.item_rv_hy, hyInfos);
                        BqaManager.setRv(emptyView, StudentMyHomeActivity.this, hyRvAdapter, rv);
                        //设置点击了拜访好友的回调
                        hyRvAdapter.setOnClickBfhy(new HyRvAdapter.OnClickBfhy() {
                            @Override
                            public void onclick(HyInfo.DataBean item) {
                                //跳转到好友的宠物家界面
                                FriednPetHomeActivity.goActivity(StudentMyHomeActivity.this, item.getName(), item.getUser_id(), item.getSex());
                                PopUpManager.dismiss();
                            }
                        });
                    }

                } else {
                    LogUtils.iTag("test", Constants.ERRORSTATE + hyInfo.getState());
                }
            }

            @Override
            public void onError(String e) {
                rlPopLoading.setVisibility(View.GONE);
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });
    }

    /**
     * 道具弹框
     */
    private void showDjPop() {

        //请求服务器的道具数据
        PopUpManager.showPop(this, R.layout.pop_gy, 0.3f, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {

                //内容布局
                llPopContent = view.findViewById(R.id.ll_content);

                //标题
                tvPopName = view.findViewById(R.id.tv_name);
                tvPopName.setText("道具");

                //关闭按钮
                ImageView lvClose = view.findViewById(R.id.lv_close);
                lvClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                    }
                });

                //列表控件
                rv = view.findViewById(R.id.rv);

                //加载框布局
                rlPopLoading = view.findViewById(R.id.rl_loading);

                //加载的空视图
                emptyView = View.inflate(StudentMyHomeActivity.this, R.layout.base_rv_common_empty_view_white_bg, null);
                emptySmg = emptyView.findViewById(R.id.tv_content);

                //请求道具的数据
                httpDj();
            }
        });
    }


    @Override
    protected void onDestroy() {
        if (null != handler) {
            handler.removeCallbacksAndMessages(null);
        }
        super.onDestroy();
    }
}
